package com.haochang.chunkvod.controller.activity.hostess;

import java.util.ArrayList;
import java.util.List;

import com.haochang.base.BaseActivity;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.utils.HelperUtils;
import com.haochang.chunkvod.app.utils.ScreenUtils;
import com.haochang.chunkvod.controller.adapter.hostess.QuickFragmentPagerAdapter;
import com.haochang.chunkvod.controller.fragment.hostess.AdvancesReceivedFragment;
import com.haochang.chunkvod.controller.fragment.hostess.InsteadChargingFragment;
import com.haochang.chunkvod.tools.vod.other.DipPxConversion;
import com.haochang.chunkvod.view.vod.popupWindow.VodPopWindow;
import com.haochang.chunkvod.view.vod.title.TitleView;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 *     @author Yangjie
 *     className AdvancesReceivedActivity
 *     created at  2016/8/23  18:08
 *     咨客 预收/代付 页面
 */
public class AdvancesReceivedAndInsteadChargingActivity extends BaseActivity
{

	private TitleView titleView;
	private AdvancesReceivedFragment fragment_1;
	private InsteadChargingFragment fragment_2;
	private ViewPager viewPager;
	private ImageView cursor;
	private TextView tv_1, tv_2;
	private Button btn_list;
	private int[] arrPosition;
	private int oldposition = -1;

	@Override
	public void onResume()
	{
		super.onResume();
		int position = HelperUtils.getHelperInstance().getIValue(AdvancesReceivedAndInsteadChargingActivity.class.getName(), 0);
		moveCursor(position, position);
		showCursor();
	}

	@Override
	public void receiveParam()
	{
	}

	@Override
	public void initData()
	{
	}

	@Override
	public void initViews()
	{
		setContentView(R.layout.activity_advances_received_and_instead_charging);
		titleView = (TitleView) findViewById(R.id.titleView);
		titleView.setTitle(R.string.advances_received).setTitleOnClickListener(new TitleView.ITitleOnClickListener()
		{
			@Override
			public void onBackClick()
			{
				finish();
				animRightToLeft();
			}

			@Override
			public void onDoneClick()
			{
				List<String> list = new ArrayList<String>();
				list.add("回到首页");
				list.add("修改密码");
				list.add("退出登录");
				int location[] = new int[2];
				ImageView img = (ImageView) titleView.findViewById(R.id.img_operates);
				img.getLocationOnScreen(location);
				int x = location[0];
				final VodPopWindow pop = new VodPopWindow(AdvancesReceivedAndInsteadChargingActivity.this, list);
				pop.showPopupWindow(titleView, x);
				pop.setPopWindowListListener(new VodPopWindow.OnPopWindowListListener()
				{
					@Override
					public void onSelectItem(int position)
					{
						switch (position)
						{
						case 0:
							backToHome();
							break;
						case 1:
							goToPasswordModify();
							break;
						case 2:
							loginOut();
							break;

						default:
							break;
						}
					}
				});
			}
		});
		initView();
		initWidth(ScreenUtils.getScreenWidth(context) / 2);
		initEvent();
		HelperUtils.getHelperInstance().setValue(AdvancesReceivedAndInsteadChargingActivity.class.getName(), 0);
		if (null == fragment_1)
			fragment_1 = new AdvancesReceivedFragment();
		if (null == fragment_2)
			fragment_2 = new InsteadChargingFragment();
		oldposition = 0;
		ArrayList<Fragment> listFragments = new ArrayList<Fragment>();
		listFragments.add(fragment_1);
		listFragments.add(fragment_2);
		QuickFragmentPagerAdapter adapter = new QuickFragmentPagerAdapter(getSupportFragmentManager(), listFragments);
		viewPager.setAdapter(adapter);
		tv_1.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
		{
			@Override
			public void onGlobalLayout()
			{
				initWidth(tv_1.getWidth());
				moveCursor(0, 0);
				tv_1.getViewTreeObserver().removeGlobalOnLayoutListener(this);
			}
		});

	}

	private void initView()
	{
		RelativeLayout relative_1 = (RelativeLayout) findViewById(R.id.relative_1);
		RelativeLayout relative_2 = (RelativeLayout) findViewById(R.id.relative_2);
		Button btn_sure = (Button) findViewById(R.id.btn_sure);
		tv_1 = (TextView) findViewById(R.id.tv_1);
		tv_2 = (TextView) findViewById(R.id.tv_2);
		cursor = (ImageView) findViewById(R.id.cursor);
		viewPager = (ViewPager) findViewById(R.id.viewPager);
		btn_list = (Button) findViewById(R.id.btn_list);

		relative_1.setOnClickListener(onBaseClickListener);
		relative_2.setOnClickListener(onBaseClickListener);
		btn_list.setOnClickListener(onBaseClickListener);
		btn_sure.setOnClickListener(onBaseClickListener);
	}

	private void initWidth(int cusorLenth)
	{
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = ScreenUtils.getScreenWidth(context);
		int num = 2;//分成几份
		int maxLenth = screenW / num;
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(cusorLenth, DipPxConversion.dip2px(context, 2));
		cursor.setLayoutParams(params);
		arrPosition = new int[] { (maxLenth - cusorLenth) / 2, (maxLenth - cusorLenth) / 2 + maxLenth };
	}

	private void showCursor()
	{
		tv_1.setTextColor(HelperUtils.getHelperInstance().getIValue(AdvancesReceivedAndInsteadChargingActivity.class.getName(), 0) == 0
				? getResources().getColor(R.color.red) : getResources().getColor(R.color.color_282828));
		tv_2.setTextColor(HelperUtils.getHelperInstance().getIValue(AdvancesReceivedAndInsteadChargingActivity.class.getName(), 0) == 1
				? getResources().getColor(R.color.red) : getResources().getColor(R.color.color_282828));
	}

	public void moveCursor(int position, int oldposition)
	{
		Animation animation = new TranslateAnimation(arrPosition[oldposition], arrPosition[position], 0, 0);
		animation.setFillAfter(true);// True:图片停在动画结束位置
		animation.setDuration(300);
		cursor.startAnimation(animation);
	}

	private void initEvent()
	{
		viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
		{
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
			{

			}

			@Override
			public void onPageSelected(int position)
			{
				moveCursor(position, oldposition);
				HelperUtils.getHelperInstance().setValue(AdvancesReceivedAndInsteadChargingActivity.class.getName(), position);
				showCursor();
				oldposition = position;
				btn_list.setText(position == 0 ? "预收列表" : "代付列表");
			}

			@Override
			public void onPageScrollStateChanged(int state)
			{

			}
		});
	}

	/**
	 * @param position 点击的position
	 */
	private void clickToMoveCusor(int position)
	{
		HelperUtils.getHelperInstance().setValue(AdvancesReceivedAndInsteadChargingActivity.class.getName(), position);
		moveCursor(position, oldposition);
		showCursor();
		viewPager.setCurrentItem(position);
	}

	OnBaseClickListener onBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			switch (v.getId())
			{
			case R.id.relative_1:
				clickToMoveCusor(0);
				break;
			case R.id.relative_2:
				clickToMoveCusor(1);
				break;
			case R.id.btn_list:
				if (TextUtils.equals(btn_list.getText().toString(), "预收列表"))
					startEnterActivity(AdvancesReceivedListActivity.class);//跳转 预收列表 页面
				else
					startEnterActivity(InsteadChargeListActivity.class);//跳转 代付列表 页面
				break;
			case R.id.btn_sure:
				if (TextUtils.equals(btn_list.getText().toString(), "预收列表"))
					fragment_1.requestAddAdvancesReceived();
				else
					fragment_2.requestAddInsteadCharging();
				break;
			default:
				break;
			}
		}
	};
}
